package com.maotai.app.thread;

import java.sql.SQLException;
import java.util.Map;

import org.apache.http.client.config.RequestConfig;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.app.bean.MaotaiAppCookie;
import com.maotai.app.bean.MaotaiAppString;
import com.maotai.app.service.MaotaiAppService;
import com.maotai.app.util.MaotaiAppHttpClientUtil;
import com.maotai.llzhou.util.IPFactory;

public class MaotaiAppOrderCheckThread extends Thread {
	private MaotaiAppService maotaiAppService;
	private Map<String, Object> zhanghao;
	private String ipAndPort;
	
	public MaotaiAppOrderCheckThread(MaotaiAppService maotaiAppService,
			Map<String, Object> zhanghao, String ipAndPort) {
		this.maotaiAppService = maotaiAppService;
		this.zhanghao = zhanghao;
		this.ipAndPort = ipAndPort;
	}

	public String getIpAndPort() {
		return ipAndPort;
	}

	public void setIpAndPort(String ipAndPort) {
		this.ipAndPort = ipAndPort;
	}

	public Map<String, Object> getZhanghao() {
		return zhanghao;
	}

	public void setZhanghao(Map<String, Object> zhanghao) {
		this.zhanghao = zhanghao;
	}

	public MaotaiAppService getMaotaiAppService() {
		return maotaiAppService;
	}

	public void setMaotaiAppService(MaotaiAppService maotaiAppService) {
		this.maotaiAppService = maotaiAppService;
	}

	@Override
	public void run() {
		try {
			MaotaiAppHttpClientUtil hcu = MaotaiAppHttpClientUtil.getInstance();
			//预约检查
			RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
			String acwTc = zhanghao.get("acwTc") + "";
			String auth = zhanghao.get("auth") + "";
			String memberid = zhanghao.get("memberid") + "";
			MaotaiAppCookie mc = new MaotaiAppCookie();
			mc.setAcwTc(acwTc);
			mc.setAuth(auth);
			mc.setMemberid(memberid);
			//查询订单
			mc = hcu.sendHttpGetProxy(MaotaiAppString.ORDER_QUERY_URL_NEW,
					mc, config, null);
			System.out.println(mc.getContent());
			if(mc.getContent()!=null && !"".equals(mc.getContent())){
				JSONObject jObject2 = JSONObject.parseObject(mc
						.getContent());
				JSONObject data2 = jObject2.getJSONObject("data");
				JSONArray data3 = data2.getJSONArray("list");
				//看订单的状态，是不是未支付的
				if (data3 == null || data3.size() == 0) {
					System.out.println("账号没有订单--");
				}else{
					for (int i = 0; i < data3.size(); i++) {
						JSONObject temp = data3.getJSONObject(i);
						JSONObject delivery = temp.getJSONObject("delivery");
						String address = delivery.getString("address");//"address": "江苏，无锡市，新区吼山南路591号龙记首饰",
						String deliveryMobile = delivery.getString("deliveryMobile");//"deliveryMobile": "13438822775",
						String deliveryName = delivery.getString("deliveryName");//"deliveryName": "朱根",
						String logisticsCompanyDesc = delivery.getString("logisticsCompanyDesc");//"logisticsCompanyDesc": "顺丰快递（贵）",
						String orderId = delivery.getString("orderId");//"orderId": 2265740,
						String waybillNo = delivery.getString("waybillNo");//"waybillNo": "620792631228"
						
						JSONObject orderMain = temp.getJSONObject("orderMain");
						String orderNo = orderMain.getString("orderNo");//"orderNo": "2018061501081",
						String orderTime = orderMain.getString("orderTime");//"orderTime": "2018-06-15 10:00:07",
						String payAmount = orderMain.getString("payAmount");//"payAmount": 7495.00,
						String payOnlineOff = orderMain.getString("payOnlineOff");//"payOnlineOff": "在线支付",
						String shopId = orderMain.getString("shopId");//"shopId": "1173773178264259584",
						String shopName = orderMain.getString("shopName");//"shopName": "茅台商城",
						String status = orderMain.getString("status");//"status": 5,
						String statusName = orderMain.getString("statusName");//"statusName": "已签收",
						zhanghao.put("address", address);
						zhanghao.put("deliveryMobile", deliveryMobile);
						zhanghao.put("deliveryName", deliveryName);
						zhanghao.put("kuaiditype", logisticsCompanyDesc);
						zhanghao.put("orderId", orderId);
						zhanghao.put("kuaidicode", waybillNo);
						zhanghao.put("orderNo", orderNo);
						zhanghao.put("orderTime", orderTime);
						zhanghao.put("payAmount", payAmount);
						zhanghao.put("payOnlineOff", payOnlineOff);
						zhanghao.put("shopId", shopId);
						zhanghao.put("shopName", shopName);
						zhanghao.put("status", status);
						zhanghao.put("statusName", statusName);
						//"statusName": "等待付款"
						if("1".equals(status)){
							maotaiAppService.insertMaotaiAppOrderItem(zhanghao);//"status": 1,
							break;
						}
					}
				}
			}
		
		} catch (Exception e) {
			e.printStackTrace();
			zhanghao.put("orderstatus", "0");
			try {
				maotaiAppService.updateMaotaiAppOrderStatus(zhanghao);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
}
